/*--------------------------------------------------------------------------------------*\
**
** adv_ferry.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_events_set_owner.h"

#include "adventure_map.h"
#include "adventure_object.h"
#include "adventure_object_ptr.h"
#include "adv_actor_action.h"
#include "actor.h"
#include "army.h"
#include "compressed_filter.h"
#include "game_time.h"
#include "owned_adv_object.h"
#include "saved_game_header.h"

namespace 
{
	enum t_set_owner_object_type
	{
		k_army						= 1,
		k_ownable_adv_object		= 2
	};

	const int k_memory_event_buffer_size	= 0;
	const int k_event_lifespan				= 0*1000;			//in mili-seconds
};

t_adventure_event_set_owner::t_adventure_event_set_owner ( ) 
: t_adventure_event_base ( k_set_owner_event ) 
{ 	
	m_owner			= -1;
}

t_adventure_event_set_owner::t_adventure_event_set_owner ( t_owned_adv_object* object, int owner ) 
: t_adventure_event_base ( k_set_owner_event ) 
{ 	
	m_global_id			= object->get_global_id();
	m_owner				= owner;
	m_previous_owner	= object->get_owner_number();
	m_object_type		= k_ownable_adv_object;

}

t_adventure_event_set_owner::t_adventure_event_set_owner( t_army* army, int owner )
: t_adventure_event_base ( k_set_owner_event ) 
{
	m_is_finished		= false;
	m_global_id			= army->get_global_id();
	m_owner				= owner;
	m_previous_owner	= army->get_owner_number();
	m_object_type		= k_army;
}
	
void t_adventure_event_set_owner::execute_event( t_adventure_map* map, t_saved_game_header const & header ) 
{
	t_adventure_object * object = map->get_adventure_object_with_gid ( m_global_id );
	
	set_owner(object,m_owner);
	
	m_time_end = get_time() + k_event_lifespan;
	
	return;
}

void t_adventure_event_set_owner::undo_event( t_adventure_map* map , t_saved_game_header const & header )
{	
	t_adventure_object * object = map->get_adventure_object_with_gid ( m_global_id );

	set_owner(object,m_previous_owner);

	return;
}

void t_adventure_event_set_owner::set_owner ( t_adventure_object * object, int owner )
{
	if (object == NULL) 
		return;
	
	switch (m_object_type)
	{
	case k_army:
		{
			t_army* dyn_cast_object = dynamic_cast<t_army*>(object);

			if (dyn_cast_object == NULL) 
				return;

			dyn_cast_object->set_owner(owner);
		}
		break;
	case k_ownable_adv_object:
		{
			t_owned_adv_object* dyn_cast_object = dynamic_cast<t_owned_adv_object*>(object);
			
			if (dyn_cast_object == NULL) 
				return;
			
			dyn_cast_object->set_owner(owner);
		}
		break;
		
	default:
		break;
	}

	return;
}

bool t_adventure_event_set_owner::read( std::streambuf & buffer , int version )
{
	m_object_type = get<int> (buffer);
	m_owner = get<int> (buffer);
	m_previous_owner = get<int> (buffer);

	return t_adventure_event_base::read( buffer , version );
}

bool t_adventure_event_set_owner::write( std::streambuf & buffer )
{	
	put<int> (buffer, m_object_type);
	put<int> (buffer, m_owner);
	put<int> (buffer, m_previous_owner);
	
	return t_adventure_event_base::write( buffer );
}
